package mysql

import (
	"gorm.io/gorm"
	"novel-kit/novelrpc/novelrpc"
)

type Comment struct {
	gorm.Model
	UserId  int    `gorm:"type:int;not null;comment:'用户id'"`
	NovelId int    `gorm:"type:int;not null;comment:'小说id'"`
	Comment string `gorm:"type:varchar(500);not null;comment:'小说评论'"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Create(c).Error
}

func CommentList(db *gorm.DB, id int64) []*novelrpc.CommentList {
	var commentList []*novelrpc.CommentList
	db.Model(&Comment{}).
		Select("comments.id as comment_id,users.nick_name,users.head_image,comments.`comment`").
		Joins("LEFT JOIN users ON comments.user_id=users.id").
		Joins("LEFT JOIN novels ON comments.novel_id=novels.id").
		Where("novels.id=?", id).Find(&commentList)
	return commentList
}
